package com.tc.dispatch.web.schedule.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.DianDuPrice;
import com.tc.dispatch.gsservice.model.ExRate;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.util.DateJsonValueProcessor;


public class huilv extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public huilv() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public boolean removeRecord(String name)
	{
		GSServiceLocal gss=gsHelper.getGSServiceLocal();
		try{
		gss.removeHuilv(name);
		}
		catch(Exception e)
		{
			System.out.println("remove error:"+e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		HttpSession sess = request.getSession();
		if(action.equals("list"))
		{
			 String page=request.getParameter("start");
		        if(page==null)
		          page="0";
		        String pagePerNum=request.getParameter("limit");
		        if(pagePerNum==null)
		          pagePerNum="10";
		        try{
		        
		        	List ret=gss.findHuilv("");
		        
		        	JSONObject json = new JSONObject();
					JsonConfig cfg = new JsonConfig();
					cfg.registerJsonValueProcessor(java.util.Date.class,
							new DateJsonValueProcessor());
					cfg.registerJsonValueProcessor(java.sql.Date.class,
							new DateJsonValueProcessor());

					// BeanUtils.copyProperties();
					json.put("totalProperty", new Integer(ret.size()));

					JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
					json.put("root", arr);

					response.getWriter().print(json);
		        }
		        catch(GSException e)
		        {
		        	
		        	System.out.println("list error:"+e.getMessage());
		        	response.getWriter().print("{success:false}");
		        }
		        
		}
		else if(action.equals("add"))
		{
			String name=request.getParameter("name");
			String huilv=request.getParameter("rate");
			String modifydate=request.getParameter("date1");
			try{
				ExRate hl=new ExRate(name,Float.parseFloat(huilv),new Date());
				gss.createHuilv(hl);
			}
			catch(GSException e)
			{
				System.out.println("create error:"+e.getMessage());
			}
			catch(Exception e)
			{
				System.out.println("exception:"+e.getMessage());
			}
			out.print("{success:true, data:'sucess'}");
		}
		else if(action.equals("update"))
		{
			String name=request.getParameter("name");
			String huilv=request.getParameter("rate");
			String modifydate=request.getParameter("date1");
			try{
				
				ExRate hl=new ExRate(name,Float.parseFloat(huilv),new Date());
				gss.updateHuilv(hl);
			}
			catch(GSException e)
			{
				System.out.println("update error:"+e.getMessage());
			}
			catch(Exception e)
			{
				System.out.println("exception:"+e.getMessage());
			}
			out.print("{success:true, data:'sucess'}");
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
